let city = ''
$.ajax({
  url: "../city.json",
  data: { action: "jQueryAjax" },
  type: "GET",
  async: false,
  success: function (data) {
    city = data
  },
  //返回数据的格式
  dataType: "json"
})
const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      rules: {
        Trading_Type: [{ required: true, message: '请选择打款方式', trigger: 'change' }],
      },
      form: {
      },//表单数据
      group_transaction_form: {
        Children: { group_transaction_paystages: [] }
      },
      formData: {
        Group_Transaction_SerialNumber: "",//主键
        Group_Purchasing_SerialNumber: "",//流水号
        Quantity: "",//总量
        Amount: "",//钱
        Submission_Time: "",//时间
        Trading_Type: "",
        Shipment_Type: "",
        Transport_Type: "",
        Payment_Type: "",
        Buyer_Contact: "",//买主姓名
        Buyer_Contact_Phone: "",//买主手机号
        Buyer_Enterprise_Info_SerialNumber: $.cookie("enterprise"),//买主公司编号
        Enterprise_Info_SerialNumber: "",//卖家公司编号
        Children: { group_transaction_paystages: [] }
      },
      // 整个省市县数据
      city,
      // 被选中的市数据
      cityList: [],
      // 被选中的县数据
      value: '',
      Trading_Type: [],//打款方式
      Shipment_Type: [],//装运方式
      Transport_Type: [],//运输方式
      Payment_Type: [],//付款方式
      areaList: [],
      selectProvince: {},
      selectCity: {},
      selectArea: {},
      address: '',
      loading: false,
      num: 1,
      span1: '',
      span2: '',
      span3: '',
      span4: '',
    }
  },
  mounted() {
    // 采购信息主键
    const Group_Purchasing_SerialNumber = sessionStorage.getItem('Group_Purchasing_SerialNumber')
    const Group_Transaction_SerialNumber = sessionStorage.getItem('Group_Transaction_SerialNumber')
    this.getData(Group_Purchasing_SerialNumber)
    this.getDetail(Group_Transaction_SerialNumber)
  },
  methods: {
    //获取上一页传来的数据
    getData(Group_Purchasing_SerialNumber) {
      let _this = this;
      $.ajax({
        url: DomainName + '/getGroupProcureIDAction',
        type: 'POST',
        data: {
          Group_Purchasing_SerialNumber,
          token: $.cookie("token")
        },
        success: function (res) {
          _this.form = res.Data
          console.log(res)
          _this.Trading_Type = res.Trading
          _this.Shipment_Type = res.Shipment
          _this.Transport_Type = res.Transport
          _this.Payment_Type = res.Payment
          _this.num = res.Data.Starting_Quantity
        }
      })
    },
    getDetail(Group_Transaction_SerialNumber) {
      console.log(Group_Transaction_SerialNumber)
      let _this = this;
      let obj = {
        // 采购信息表
        group_transaction: {
          group_transaction_paystages: {}
        }
      }
      $.ajax({
        url: DomainName + '/getSubtableInquiryOriginal',
        type: 'POST',
        data: {
          Primary_Key: Group_Transaction_SerialNumber,
          JSONObject: JSON.stringify(obj),
          token: $.cookie("token")
        },
        success: function (res) {
          _this.formData = res.group_transaction[0]
          console.log(res)
        }
      })
    },
    // 生成日志
    submitForm() {
      console.log(this.form)
      let year = new Date().getFullYear();
      let month = new Date().getMonth() + 1;
      let days = new Date().getDate();
      let hour = new Date().getHours()
      let minutes = new Date().getMinutes()
      let second = new Date().getSeconds()
      if (month < 10) { month = '0' + month }
      if (days < 10) { days = '0' + days }
      if (hour < 10) { hour = '0' + hour }
      if (minutes < 10) { minutes = '0' + minutes }
      if (second < 10) { second = '0' + second }
      var times = year + '-' + month + '-' + days + ' ' + hour + ':' + minutes + ':' + second
      this.formData.Submission_Time = times
      this.formData.Enterprise_Info_SerialNumber = this.form.Enterprise_Info_SerialNumber
      this.formData.Group_Purchasing_SerialNumber = this.form.Group_Purchasing_SerialNumber
      this.formData.Quantity = this.num
      this.formData.Amount = this.num * this.form.Amount
      console.log(this.formData)
      var reg = /^1[3-9]\d{9}$/
      if (this.formData.Buyer_Contact == '') {
        this.$message({
          message: '请输入联系人',
          type: 'warning'
        });
        return
      }
      if (this.formData.Buyer_Contact_Phone == '') {
        this.$message({
          message: '请输入联系人手机号',
          type: 'warning'
        });
        return
      }
      if (!reg.test(this.formData.Buyer_Contact_Phone)) {
        this.$message({
          message: '手机号输入有误,请仔细检查',
          type: 'warning'
        });
        return
      }
      if (this.formData.Trading_Type == '') {
        this.$message({
          message: '请选择打款方式',
          type: 'warning'
        });
        return
      }
      if (this.formData.Shipment_Type == '') {
        this.$message({
          message: '请选择装运方式',
          type: 'warning'
        });
        return
      }
      if (this.formData.Transport_Type == '') {
        this.$message({
          message: '请选择运输方式',
          type: 'warning'
        });
        return
      }
      if (this.formData.Payment_Type == '') {
        this.$message({
          message: '请选择付款方式',
          type: 'warning'
        });
        return
      }
      let _this = this;
      _this.loading = true
      if (_this.loading) {
        var group_message_buyer = {
          Group_Message_SerialNumber: "",
          Group_Transaction_SerialNumber: _this.formData.Group_Transaction_SerialNumber,
          Cause: "确认合同",
          Submission_Time: times,
          Enterprise_Info_SerialNumber: $.cookie("enterprise"),
          Seller_Type: "0"
        }
        _this.formData.Row_Status = '4'
        let data = { Data: JSON.stringify({ Modify: { group_transaction: [_this.formData] }, Add: { group_message: [group_message_buyer] } }), token: $.cookie("token") }
        $.ajax({
          url: DomainName + '/getEditAction',
          type: 'POST',
          data,
          success: function (res) {
            console.log('提交结果', res)
            _this.$message.success("已确认合同")
            window.history.go(-1);
          },
          error: function () {
            _this.$message.error("操作失败，请稍后再试")
          },
          complete: function (XMLHttpRequest) {
            _this.loading = false
            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
              window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
            }
          }
        })
      }
    },
    // // 检验库存
    // Inventory(Group_Purchasing_SerialNumber) {
    //   let _this = this
    //   _this.loading = true
    //   console.log(Group_Purchasing_SerialNumber, _this.num)
    //   $.ajax({
    //     url: DomainName + '/GroupCheckStockAction',
    //     type: 'POST',
    //     data: {
    //       Group_Purchasing_SerialNumber,
    //       Quantity: _this.num,
    //       token: $.cookie("token")
    //     },
    //     success: function (res) {
    //       console.log(res)
    //       if (res.Status == 'success') {
    //         _this.submitForm()
    //         // _this.$notify({
    //         //   title: '成功',
    //         //   message: '企业审核中,请等待',
    //         //   type: 'success'
    //         // });
    //       } else {
    //         _this.$notify.error({
    //           title: '错误',
    //           message: '库存不足'
    //         });
    //       }
    //     },
    //     complete: function (XMLHttpRequest) {
    //       if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
    //         window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
    //       }
    //     },
    //   })
    // },
    // // 提交表单
    // submitMessage(data) {
    //   var _this = this
    //   $.ajax({
    //     url: DomainName + '/getAddAction',
    //     type: 'POST',
    //     data: {
    //       Data: JSON.stringify({ "group_message": data }),
    //       token: $.cookie("token")
    //     },
    //     success: function (res) {
    //       _this.loading = false
    //       console.log(res)
    //       if (res.Status == 'success') {
    //         _this.$notify({
    //           title: '成功',
    //           message: '企业审核中,请等待',
    //           type: 'success'
    //         });
    //         window.history.go(-1);
    //       } else {
    //         _this.$notify.error({
    //           title: '错误',
    //           message: '系统错误..'
    //         });
    //       }
    //     }
    //   })

    // },
    //   changenumber(e) {
    //     this.formData.Payment_Type = []
    //     this.span4 = ''
    //   },
    //   change1(e) {
    //     console.log(e)
    //     if (e == '聚合支付') {
    //       this.span1 = '第三方支付交易'
    //     } else {
    //       this.span1 = '指定账号打款'
    //     }
    //   },
    //   change2(e) {
    //     console.log(e)
    //     if (e == '整装') {
    //       this.span2 = '整装打包'
    //     } else {
    //       this.span2 = '散装打包'
    //     }
    //   },
    //   change3(e) {
    //     console.log(e)
    //     if (e == '海运') {
    //       this.span3 = '海运'
    //     } else if (e == '空运') {
    //       this.span3 = '空运'
    //     } else if (e == '陆运') {
    //       this.span3 = '陆运'
    //     } else {
    //       this.span3 = '铁路'
    //     }
    //   },
    //   change4(e) {
    //     console.log(e)
    // let year = new Date().getFullYear();
    // let month = new Date().getMonth() + 1;
    // let days = new Date().getDate();
    // let hour = new Date().getHours()
    // let minutes = new Date().getMinutes()
    // let second = new Date().getSeconds()
    // if (month < 10) { month = '0' + month }
    // if (days < 10) { days = '0' + days }
    // if (hour < 10) { hour = '0' + hour }
    // if (minutes < 10) { minutes = '0' + minutes }
    // if (second < 10) { second = '0' + second }
    // var times = year + '-' + month + '-' + days + ' ' + hour + ':' + minutes + ':' + second
    //     if (e == '分期') {
    //       this.span4 = `鉴于甲方与乙方于${times}签订了产品销售合作协议，根据协议约定乙方应于${this.form.Delivery_Time}前全部支付完货款人民币${this.num * this.form.Amount}元，但至今乙方全部未支付，因此乙方于此确认结欠甲方货款人民币${this.num * this.form.Amount}元，且乙方承诺同意按以下方式分期偿还，现双方就还款达成协议。`
    //     } else {
    //       this.span4 = "全款"
    //     }
    //   },
    //   changed(e) {
    //     console.log(e)
    //   },
    //   // 省份 市 县联动
    //   selectProvinceFun(event) {
    //     console.log(event)
    //     if (event) {
    //       this.cityList = event.children
    //     } else {
    //       this.cityList = []
    //     }
    //     this.areaList = []
    //   },
    //   selectCityFun(event) {
    //     console.log(event)
    //     if (event) {
    //       this.areaList = event.children
    //     } else {
    //       this.areaList = []
    //     }
    //   },
    //   selectAreaFun(event) {
    //     if (event) {
    //       console.log(event)
    //       this.isdisabled = false
    //     } else {
    //       this.isdisabled = true
    //     }
    //   },
    //   selectAddress(event) {
    //     if (event && event != '' && this.selectArea.label != undefined) {
    //       this.form.Supplier_Address = this.selectProvince.label + '/'
    //         + this.selectCity.label + '/'
    //         + this.selectArea.label + '/'
    //         + this.address
    //     }
    //   },
  }
})